package com.yunxin.customui.service.builder.echart;

import com.yunxin.customui.bean.echart.ChartBean;
import com.yunxin.customui.bean.echart.ChartSqlConfig;
import com.yunxin.customui.bean.echart.ChartValCfgBean;
import com.yunxin.customui.myinterface.chart.IChartBuilder;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class SqlChartBuilder implements IChartBuilder<ChartSqlConfig,List<Map<String,Object>>> {

    @Override
    public ChartData buildChart(ChartBean chartBean, ChartSqlConfig config, List<Map<String, Object>> mapList) {
        String xField = config.getXField();
        List<String> yFileds = config.getChartValCfgBeans().stream().map(ChartValCfgBean::getDataID).collect(Collectors.toList());
        Map<String,List<String>> chartDataListMap = new HashMap<>();
        chartDataListMap.put(xField,new ArrayList<>());
        for (String yFiled : yFileds) {
            chartDataListMap.put(yFiled,new ArrayList<>());
        }
        Set<String> strings = chartDataListMap.keySet();
        for (Map<String, Object> rowData : mapList) {
            for (String field : strings) {
                chartDataListMap.get(field).add(String.valueOf(rowData.get(field)));
            }
        }
        return new ChartData(chartDataListMap);
    }

}
